package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchasingInformationDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchasingInformation;
import com.guodu.entity.PurchasinginforStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchasingInformationService;
import com.guodu.vo.PurchasingInformationQueryVo;
import com.guodu.vo.PurchasingInformationVo;


@Service("purchasingInformationService")
public class PurchasingInformationServiceImpl implements PurchasingInformationService {
	
	
	@Resource
	private PurchasingInformationDao purchasingInformationDao;

	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	

	@Override
	public long findSizeByVo(PurchasingInformationQueryVo piq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		return purchasingInformationDao.findSizeByVo(piq, units,userInfo);
	}

	@Override
	public List<PurchasingInformationVo> findByVo(
			PurchasingInformationQueryVo piq, Units units,UserInfo userInfo, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		List<PurchasingInformation> list = purchasingInformationDao.findByVo(piq,units,userInfo, curPage, pageSize);
		List<PurchasingInformationVo> purchasingInformationVos = new ArrayList<PurchasingInformationVo>();
		for(PurchasingInformation purchasingInformation : list ){
			PurchasingInformationVo purchasingInformationVo = new PurchasingInformationVo(purchasingInformation);
			UserInfo createUser = userInfoDao.findById(purchasingInformation.getCreatePeople());
			if(purchasingInformation.getUnitsId()!=null){
			Units u = unitsDao.findById(purchasingInformation.getUnitsId());
				if(u.getUnitsLevel()==2L){
					Units fu = unitsDao.findById(u.getFunitsId());
					purchasingInformationVo.setFunitsName(fu.getUnitsName());
					purchasingInformationVo.setUnitsName(u.getUnitsName());
					purchasingInformationVo.setIndustry(u.getIndustry());
				}else{
					purchasingInformationVo.setFunitsName(u.getUnitsName());
				}
			}
			if (createUser.getUnitsId()!=null) {
				Units unitsInfos = unitsDao.findById(createUser.getUnitsId());
				purchasingInformationVo.setIsComplete(unitsInfos.getIsComplete());
				purchasingInformationVo.setUnitsLevel(unitsInfos.getUnitsLevel());
			}else{
				Long id = null;
				purchasingInformationVo.setUnitsLevel(id);
			}
			purchasingInformationVo.setUserName(createUser.getUserName());
			purchasingInformationVos.add(purchasingInformationVo);
		}
		return purchasingInformationVos;
	}

	@Override
	public void save(PurchasingInformation purchasingInformation) {
		// TODO Auto-generated method stub
		purchasingInformationDao.save(purchasingInformation);
	}

	@Override
	public PurchasingInformation findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingInformationDao.findById(purchaseId);
	}

	@Override
	public void update(PurchasingInformation purchasingInformation,Long status) {
		// TODO Auto-generated method stub
		purchasingInformationDao.update(purchasingInformation);
		List<PurchasinginforStatus> entities = new ArrayList<PurchasinginforStatus>();
		PurchasinginforStatus entity = new PurchasinginforStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchasingInformation.getCreatePeople());
		entity.setPurchaseId(purchasingInformation.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchasingInformationDao.changeStatus(entities);
	}

	@Override
	public List<PurchasinginforStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchasingInformationDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchasinginforStatus> entities = new ArrayList<PurchasinginforStatus>();
		for(int i=0;i<idses.length;i++){
			PurchasinginforStatus entity = new PurchasinginforStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchasingInformationDao.changeStatus(entities);
		purchasingInformationDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchasingInformationDao.delByIds(delIds);
	}

	@Override
	public List<PurchasingInformation> findAllByVo(
			PurchasingInformationQueryVo piq, Units attribute,UserInfo userInfo) {
		// TODO Auto-generated method stub
		return purchasingInformationDao.findAllByVo(piq,attribute,userInfo);
	}

	
	public PurchasingInformationVo findAllMoney(
			PurchasingInformationQueryVo pbpq, Units units, UserInfo userInfo) {
		
		return purchasingInformationDao.findAllMoney(pbpq,units,userInfo);
	}

	@Override
	public void examinAll(PurchasingInformationQueryVo pbpq, Units units,
			UserInfo userInfo) {
		purchasingInformationDao.examinAll(pbpq, units, userInfo);
		
	}

}
